<template>
  <el-dialog
    :title="!dataForm.houseId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="金额（/平方）" prop="housePrice">
      <el-input v-model="dataForm.housePrice" placeholder="金额（/平方）"></el-input>
    </el-form-item>
    <el-form-item label="房屋名" prop="houseName">
      <el-input v-model="dataForm.houseName" placeholder="房屋名"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="houseAddress">
      <el-input v-model="dataForm.houseAddress" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="面积（m2)-" prop="houseArea">
      <el-input v-model="dataForm.houseArea" placeholder="面积（m2)-"></el-input>
    </el-form-item>
    <el-form-item label="状态(0.已卖 1.未卖)" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态(0.已卖 1.未卖)"></el-input>
    </el-form-item>
    <el-form-item label="户型(如1室1厅)" prop="houseDesc">
      <el-input v-model="dataForm.houseDesc" placeholder="户型(如1室1厅)"></el-input>
    </el-form-item>
    <el-form-item label="默认图片" prop="defaultImg">
      <el-input v-model="dataForm.defaultImg" placeholder="默认图片"></el-input>
    </el-form-item>
    <el-form-item label="发布经纪人" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="发布经纪人"></el-input>
    </el-form-item>
    <el-form-item label="标识（1，新房 2.二手房）" prop="flag">
      <el-input v-model="dataForm.flag" placeholder="标识（1，新房 2.二手房）"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          houseId: 0,
          housePrice: '',
          houseName: '',
          houseAddress: '',
          houseArea: '',
          status: '',
          houseDesc: '',
          defaultImg: '',
          userId: '',
          flag: '',
          createTime: ''
        },
        dataRule: {
          housePrice: [
            { required: true, message: '金额（/平方）不能为空', trigger: 'blur' }
          ],
          houseName: [
            { required: true, message: '房屋名不能为空', trigger: 'blur' }
          ],
          houseAddress: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          houseArea: [
            { required: true, message: '面积（m2)-不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(0.已卖 1.未卖)不能为空', trigger: 'blur' }
          ],
          houseDesc: [
            { required: true, message: '户型(如1室1厅)不能为空', trigger: 'blur' }
          ],
          defaultImg: [
            { required: true, message: '默认图片不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '发布经纪人不能为空', trigger: 'blur' }
          ],
          flag: [
            { required: true, message: '标识（1，新房 2.二手房）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.houseId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.houseId) {
            this.$http({
              url: this.$http.adornUrl(`/house/newhouse/info/${this.dataForm.houseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.housePrice = data.newHouse.housePrice
                this.dataForm.houseName = data.newHouse.houseName
                this.dataForm.houseAddress = data.newHouse.houseAddress
                this.dataForm.houseArea = data.newHouse.houseArea
                this.dataForm.status = data.newHouse.status
                this.dataForm.houseDesc = data.newHouse.houseDesc
                this.dataForm.defaultImg = data.newHouse.defaultImg
                this.dataForm.userId = data.newHouse.userId
                this.dataForm.flag = data.newHouse.flag
                this.dataForm.createTime = data.newHouse.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/newhouse/${!this.dataForm.houseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'houseId': this.dataForm.houseId || undefined,
                'housePrice': this.dataForm.housePrice,
                'houseName': this.dataForm.houseName,
                'houseAddress': this.dataForm.houseAddress,
                'houseArea': this.dataForm.houseArea,
                'status': this.dataForm.status,
                'houseDesc': this.dataForm.houseDesc,
                'defaultImg': this.dataForm.defaultImg,
                'userId': this.dataForm.userId,
                'flag': this.dataForm.flag,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
